﻿using System;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace YArchitech.Plumbing.Model
{
	public class ColumnModel
	{
		public ColumnModel(Element ele, Autodesk.Revit.DB.Document doc, Transform t, double z, YJKViewRange floorRange, RectangleRange rectRange)
		{
			this.column = ele;
			BoundingBoxXYZ boundingBoxXYZ = ele.get_BoundingBox(doc.ActiveView);
			try
			{
				double num = boundingBoxXYZ.Max.X + (double)this.EdgeDistance / 304.8;
				double num2 = boundingBoxXYZ.Min.X - (double)this.EdgeDistance / 304.8;
				double num3 = boundingBoxXYZ.Max.Y + (double)this.EdgeDistance / 304.8;
				double num4 = boundingBoxXYZ.Min.Y - (double)this.EdgeDistance / 304.8;
				double z2 = boundingBoxXYZ.Max.Z;
				double z3 = boundingBoxXYZ.Min.Z;
				XYZ xyz = boundingBoxXYZ.Min;
				XYZ xyz2 = boundingBoxXYZ.Max;
				if (t != null)
				{
					xyz += t.Origin;
					xyz2 += t.Origin;
				}
				xyz += new XYZ((double)(-(double)this.EdgeDistance) / 304.8, (double)(-(double)this.EdgeDistance) / 304.8, 0.0);
				xyz2 += new XYZ((double)this.EdgeDistance / 304.8, (double)this.EdgeDistance / 304.8, 0.0);
				if (this.IsInCurFloorRect(xyz2, xyz, z, floorRange, rectRange))
				{
					if (t == null)
					{
						this.rr = new RectangleRange(new XYZ(num2, num3, 0.0), new XYZ(num, num3, 0.0), new XYZ(num, num4, 0.0), new XYZ(num2, num4, 0.0));
					}
					else
					{
						this.rr = new RectangleRange(new XYZ(num2, num3, 0.0) + t.Origin - new XYZ(0.0, 0.0, t.Origin.Z), new XYZ(num, num3, 0.0) + t.Origin - new XYZ(0.0, 0.0, t.Origin.Z), new XYZ(num, num4, 0.0) + t.Origin - new XYZ(0.0, 0.0, t.Origin.Z), new XYZ(num2, num4, 0.0) + t.Origin - new XYZ(0.0, 0.0, t.Origin.Z));
					}
				}
				else
				{
					this.rr = null;
				}
			}
			catch
			{
				this.rr = null;
			}
		}

		public bool IsInCurFloorRect(XYZ maxPoint, XYZ minPoint, double z, YJKViewRange floorRange, RectangleRange rectRang)
		{
			if (!rectRang.InsidePolygon(maxPoint) && !rectRang.InsidePolygon(minPoint))
			{
				return false;
			}
			if (floorRange.floorM != YJKViewRange.FloorMark.MidFloor && floorRange.floorM == YJKViewRange.FloorMark.TopFloor)
			{
				return false;
			}
			double num = z - 0.32808398950131235;
			if (floorRange.floorM == YJKViewRange.FloorMark.MidFloor)
			{
				double num2 = z + floorRange.height - 0.32808398950131235;
				if (maxPoint.Z > num && maxPoint.Z > num2 && minPoint.Z > num && minPoint.Z > num2)
				{
					return false;
				}
				if (maxPoint.Z < num && maxPoint.Z < num2 && minPoint.Z < num && minPoint.Z < num2)
				{
					return false;
				}
			}
			return floorRange.floorM != YJKViewRange.FloorMark.TopFloor || maxPoint.Z >= num || minPoint.Z >= num;
		}

		public RectangleRange rr;

		public Element column;

		public int EdgeDistance = 100;
	}
}
